﻿using System;
using System.Collections.Generic;
using System.Text;
using Dominio;

namespace Persistencia
{
    class BrokerMedioDePago: Broker
    {
        #region constructor

        public BrokerMedioDePago()
        {
            this.conn = ConexionBD.getInstance();
        }

        #endregion

        #region methods

        public override void add(Object o)
        {
            MedioDePago MedioAux = (MedioDePago)o;
            this.conn.connect();
            string sql = "insert into MediosDePago(Nombre) values('"
                + MedioAux.Nombre + "')";
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void update(Object o)
        {
            MedioDePago MedioAux = (MedioDePago)o;
            string sql = "update MediosDePago set Nombre = '" + MedioAux.Nombre
                + "' where idMedioDePago = " + MedioAux.OID;
            this.conn.connect();
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void delete(Object o)
        {
            MedioDePago MedioAux = (MedioDePago)o;
            string sql = "delete * from MediosDePago where idMedioDePago = " + MedioAux.OID;
            this.conn.connect();
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void bringData(Object o)
        {
            MedioDePago MedioAux = (MedioDePago)o;
            this.conn.connect();
            string sql = "Select * from MediosDePago where idMedioDePago = " + MedioAux.OID;
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {
                MedioAux.Nombre = this.conn.getDataString("Nombre").ToString();                
            }
            this.conn.disconnect();
        }

        public override List<Object> bringList(Object o)
        {
            string sql = "Select * from MediosDePago";
            List<Object> list = new List<Object>();
            this.conn.connect();
            this.conn.bringList(sql);
            while (this.conn.nextRecord())
            {
                MedioDePago MedioAux = new MedioDePago();
                MedioAux.Nombre = this.conn.getDataString("Nombre").ToString();
                MedioAux.OID = int.Parse(this.conn.getDataString("idMedioDePago").ToString());
                list.Add(MedioAux);
            }
            this.conn.disconnect();
            return list;
        }

        public override int bringLastId()
        {
            int id = 0;
            try
            {
                this.conn.connect();
                string sql = "Select max(idMedioDePago) as idMedio from MediosDePago";
                this.conn.bringData(sql);
                while (this.conn.nextRecord())
                {
                    id = int.Parse(this.conn.getDataString("idMedio").ToString());
                }
                this.conn.disconnect();
            }
            catch (Exception)
            {
                this.conn.disconnect();
                id = 0;
            }            
            return id;
        }

        #endregion
    }
}
